package com.huiw.archives.common.utils.http;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;

import com.google.gson.Gson;
import com.huiw.archives.common.utils.database.RequestParams;

/**
 * @author YC<br>
 *         进行http请求工具类
 */
public class HttpsUtil {

	/**
	 * 
	 * YC 2017年7月19日 上午11:01:22
	 * <p>
	 * Title: httpRequest
	 * </p>
	 * <p>
	 * Description: http访问
	 * </p>
	 * 
	 * @param requestUrl
	 *            访问url
	 * @param requestMethod
	 *            访问方式 (GET、POST)
	 * @param message
	 *            内容信息(GET访问为null)
	 * @return
	 * @throws Exception
	 */
	private static String httpRequest(String requestUrl, String requestMethod, String message) throws Exception {
		HttpURLConnection conn = null;
		BufferedReader bufferedReader = null;
		try {
			URL url = new URL(requestUrl);
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod(requestMethod);
			conn.setRequestProperty("content-type", "application/json");
			if (null != message) {
				OutputStream outputStream = conn.getOutputStream();
				outputStream.write(message.getBytes("utf-8"));
				outputStream.close();
			}
			bufferedReader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			return buffer.toString();
		} catch (Exception e) {
			return "httpRequest方法异常";
		} finally {
			if (conn != null) {
				conn.disconnect();
			}
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					return "httpRequest方法异常";
				}
			}
		}
	}

	/**
	 * 
	 * YC 2017年8月2日 下午3:20:04
	 * <p>
	 * Title: GET
	 * </p>
	 * <p>
	 * Description:以get方式发出请求
	 * </p>
	 * 
	 * @param requestUrl
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String GET(String requestUrl, Map<String, Object> map) throws Exception {
		StringBuffer sb = new StringBuffer();// 存储参数
		String params = "";// 编码之后的参数
		// 编码请求参数
		if (map.size() == 1) {
			for (String name : map.keySet()) {
				sb.append(name).append("=").append(URLEncoder.encode(map.get(name).toString(), "UTF-8"));
			}
			params = sb.toString();
		} else {
			for (String name : map.keySet()) {
				sb.append(name).append("=").append(URLEncoder.encode(map.get(name).toString(), "UTF-8")).append("&");
			}
			String temp_params = sb.toString();
			params = temp_params.substring(0, temp_params.length() - 1);
		}

		return httpRequest(requestUrl + "?" + params, "GET", null);

	}

	/**
	 * 
	 * YC 2017年8月2日 下午3:20:49
	 * <p>
	 * Title: POST
	 * </p>
	 * <p>
	 * Description: 以post方式发出请求
	 * </p>
	 * 
	 * @param requestUrl
	 * @param map
	 * @return
	 * @throws Exception
	 */
	public static String POST(String requestUrl, Map<String, Object> map) throws Exception {
		return httpRequest(requestUrl, "POST", new Gson().toJson(map));

	}

	/**
	 * 
	 * YC 2017年8月2日 下午3:20:49
	 * <p>
	 * Title: POST
	 * </p>
	 * <p>
	 * Description: 以post方式发出请求
	 * </p>
	 * 
	 * @param requestUrl
	 *            url
	 * @param params
	 *            自定义request请求体
	 * @return
	 * @throws Exception
	 */
	public static String POST(String requestUrl, RequestParams params) throws Exception {
		return httpRequest(requestUrl, "POST", new Gson().toJson(params));

	}

}
